﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLKCB.Controller;
using QLKCB.Common;

namespace QLKCB
{
    public partial class Form_DangKyKhamBenh : Form
    {

        cBenhNhan benhNhanController = new cBenhNhan();
        cDangKyKhamBenh dangKyController = new cDangKyKhamBenh();
        cPhieuThu phieuThuController = new cPhieuThu();
        Utils utils = new Utils();
        private string maBN;

        public Form_DangKyKhamBenh()
        {
            InitializeComponent();
        }

        public Form_DangKyKhamBenh(string _maBN)
        {
            maBN = _maBN;
            InitializeComponent();
        }

        public DataTable loadUser(string maBN)
        {
            DataTable dtBenhNhan = benhNhanController.getMotBenhNhan(maBN);
            return dtBenhNhan;
        }

        public void showInfoGui()
        {
            DataTable dtBenhNhan = loadUser(maBN);
            txtMaBN.Text = maBN;
            txtTenBN.Text = dtBenhNhan.Rows[0]["TenBN"].ToString();
            txtDoiTuongBN.Text = utils.getDoiTuong()[dtBenhNhan.Rows[0]["DoiTuong"]].ToString();
            cbbLoaiKham.DataSource = dangKyController.getLoaiKham();
            cbbLoaiKham.DisplayMember = "TenLoaiKham";
            cbbLoaiKham.ValueMember = "MaLoaiKham";

            DateTime today = DateTime.Today;
            lblMaDKKB.Text = dangKyController.getMaKhamBenh();
            lblNgayLapDKKB.Text = today.ToString("dd/MM/yyyy");
        }

        private void Form_DangKyKhamBenh_FormClosed(object sender, FormClosedEventArgs e)
        {
            Form_QLKhamChuaBenh form = new Form_QLKhamChuaBenh();
            form.Show();
            this.Hide();
        }

        private void Form_DangKyKhamBenh_Load(object sender, EventArgs e)
        {
            showInfoGui();
        }

        private void btnInPhieuDKKB_Click(object sender, EventArgs e)
        {

            string maKhamBenh = dangKyController.getMaKhamBenh();
            string maPhieuThu = phieuThuController.getMaPhieuThu();
            string maBN = txtMaBN.Text;
            string maLoaiKham = cbbLoaiKham.SelectedValue.ToString();
            DateTime today = DateTime.Today;
            string chuanDoan = txtChanDoanBanDau.Text;

            string lyDoThu = "Đăng ký khám bệnh (" + cbbLoaiKham.SelectedText + ")";
            int resultPhieuThu = phieuThuController.insertPhieuThu(maPhieuThu, lyDoThu);
            if (resultPhieuThu > 0)
            {
                int resultKhamBenh = dangKyController.insertPhieuKhamBenh(maPhieuThu, maBN, maLoaiKham, today, chuanDoan);
                if (resultKhamBenh > 0)
                {
                    MessageBox.Show("Đăng ký khám bệnh thành công.");
                }
                else
                {
                    MessageBox.Show("Đăng ký khám bệnh không thành công.");
                }
            }
            else
            {
                MessageBox.Show("Đăng ký khám bệnh không thành công.");
            }

        }
    }
}
